package design_pattern.observer;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by WeiDingKuo on 2017/10/26 20:
 * <p>
 * 被观察者
 */
public class ConcreteSubject implements Subject {
    private Set<Observer> observerSet = new HashSet();

    @Override
    public void registerObserver(Observer o) {
        if (!observerSet.contains(o)) {
            observerSet.add(o);
        }
    }

    @Override
    public void removeObserver(Observer o) {
        if (observerSet.contains(o)) {
            observerSet.remove(o);
        }
    }

    @Override
    public void notifyObserver() {
        observerSet.forEach(observer -> observer.dosomething());
    }

    public void dosomething() {
        System.out.println(this.getClass().getName() + " : do something");
        this.notifyObserver();
    }
}
